import { appContext } from "@/models/app.context";
import { appNotify, NotifyOption, NotifyType } from "@/models/app.notify";
import { appRef } from "@/models/app.ref";
import { NoteInfo, ThumbType } from "@/models/Note";
import { MessagePlugin } from "tdesign-vue-next";
import { BaseServices } from "./base.services";
import { APIUrl, APIMethod, RespInfo } from "./request.services";


export class NoteService extends BaseServices {
  private static _ins: NoteService = new NoteService();
  public static get ins(): NoteService {
    return this._ins ? this._ins : this._ins = new NoteService();
  }

  /** 添加一個便签 */
  public add(remark: string, bgcolor: string): Promise<void> {
    const url = APIUrl.Note + APIMethod.Add;
    const { uid } = appRef.user.data
    const data = { user: uid, remark, bgcolor };
    return new Promise<void>((reslove, reject) => {
      this.request(url, data).then(async (res: RespInfo<NoteInfo>) => {
        appRef.note.addNote(res.info);
        MessagePlugin("success", "添加成功");
        reslove();
      })
    });
  }
  /** 删除一個便签 */
  public delete(uid: string): Promise<void> {
    const url = APIUrl.Note + APIMethod.Delete;
    const data = { uid };
    return new Promise<void>((reslove, reject) => {
      this.request(url, data).then(async (res: RespInfo<NoteInfo>) => {
        appRef.note.deleteNote(uid);
        MessagePlugin("success", "删除成功");
        appNotify.send(NotifyType.Note, NotifyOption.Delete)
        reslove();
      })
    });
  }

  public list(page: number = 0, number: number = 0): Promise<RespInfo<NoteInfo>> {
    const url = APIUrl.Note + APIMethod.List;
    const data = { page, number };
    return new Promise<RespInfo<NoteInfo>>((reslove, reject) => {
      this.request(url, data).then(async (res: RespInfo<NoteInfo>) => {
        appRef.note.setList(res.list, res.total);
        reslove(res);
      })
    });
  }

  /** 获取详情 */
  public getOne(uid: string): Promise<NoteInfo> {
    const url = APIUrl.Note + APIMethod.Get;
    const user = appRef.user?.data?.uid
    const data = { uid, user };
    return new Promise<NoteInfo>((reslove, reject) => {
      this.request(url, data).then(async (res: RespInfo<NoteInfo>) => {
        reslove(res.info);
      })
    });
  }

  public mineList(page: number = 0, number: number = 0): Promise<NoteInfo[]> {
    const url = APIUrl.Note + APIMethod.List;
    const user = appRef.user?.data?.uid;
    const data = { user, page, number };
    return new Promise<NoteInfo[]>((reslove, reject) => {
      this.request(url, data).then(async (res: RespInfo<NoteInfo>) => {
        reslove(res.list);
      })
    });
  }
  public thumb(type: ThumbType, target: string): Promise<{ type: ThumbType, count: number }> {
    const url = APIUrl.Thumb + APIMethod.Updated;
    const user = appRef.user?.data?.uid;
    const data = { type, user, target };
    return new Promise<{ type: ThumbType, count: number }>((reslove, reject) => {
      this.request(url, data).then(async (res: RespInfo<{ type: ThumbType, count: number }>) => {
        reslove(res.info);
      })
    });
  }
  /** 分享 */
  public share(uid: string): Promise<void> {
    const url = APIUrl.Note + 'share';
    const data = { uid };
    return new Promise<void>((reslove, reject) => {
      this.request(url, data).then(async (res: RespInfo<void>) => {
        reslove();
      })
    });
  }
}

export const noteService: NoteService = NoteService.ins;